package com.tulele.vtool.handler;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tulele.vtool.model.*;
import com.tulele.vtool.model.combinedEntity.SlideshowCombinedEntity;
import com.tulele.vtool.model.reqEntity.GetArticleDetailInfoReqBean;
import com.tulele.vtool.model.reqEntity.LogDateBase;
import com.tulele.vtool.model.reqEntity.UserVipRealmLinkReqBean;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.*;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
public class SysGzhHandler {

    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private ShieldService shieldService;

    @Autowired
    private IArticleInfoService articleInfoService;

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 20200515 yl
     * 查询三个清除缓存链接
     *
     * @param
     * @return
     */
    @PostMapping(value = "/selectCacheLinkByGzhCode", produces = "application/json;charset=UTF-8")
    public RespData selectCacheLinkByGzhCode(@RequestHeader(value = "userId", required = false) String userId, @RequestHeader("identifying") String springProfilesActive) {
        SysGzh sysGzh = this.sysGzhService.selectCacheLinkByGzhCode(springProfilesActive);
        List lists = Lists.newArrayList();
        String[] res = null;
        if (StringUtils.isNotBlank(sysGzh.getRealmMessage())) {
            lists.add(sysGzh.getRealmMessage());
        }
        if (StringUtils.isNotBlank(sysGzh.getRealmName())) {
            lists.add(sysGzh.getRealmName());
        }
        if (StringUtils.isNotBlank(sysGzh.getRealmResource())) {
            lists.add(sysGzh.getRealmResource());
        }
        if (CollectionUtils.isNotEmpty(lists)) {
            res = (String[]) lists.stream().toArray(String[]::new);
        }

        List<SysRealmMessage> sysRealmMessageList = sysGzhService.getRealmByGzhCodeList(springProfilesActive);
        if (sysRealmMessageList != null && sysRealmMessageList.size() > 0) {
            for (SysRealmMessage srm : sysRealmMessageList) {
                lists.add(srm.getRealm());
            }
        }

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(res);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20210525 yl
     * 查询落地域名
     *
     * @param
     * @return
     */
    @PostMapping(value = "/getSysRealmLink", produces = "application/json;charset=UTF-8")
    public RespData getSysRealmLink(@RequestHeader(value = "userId", required = false) String userId, @RequestHeader("identifying") String springProfilesActive) {

        //PlatformSysInfo platformSysInfo = sysGzhService.getPlatformSysInfo(springProfilesActive);
        SysRealmMessage sysRealmMessage = this.sysGzhService.getRealmByGzhCode(springProfilesActive);

        String pmlink = "";
        //String mainlink="";

        if (sysRealmMessage != null) {
            pmlink = sysRealmMessage.getRealm();
        }
        //mainlink=platformSysInfo.getShareDomainUrl();

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("pmlink", pmlink);//落地域名

        //log.error("pmlink:"+pmlink);
        //resultMap.put("mainlink",mainlink);//主域名

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 根据会员状态获取公众号的访问域名
     * @param userId 用户 ID
     * @param springProfilesActive 公众号标识
     * @return
     */
    @PostMapping(value = "/getUserVipRealmLink", produces = "application/json;charset=UTF-8")
    public RespData getUserVipRealmLink(@RequestHeader(value = "userId", required = false) String userId, @RequestHeader("identifying") String springProfilesActive, @RequestBody UserVipRealmLinkReqBean userVipRealmLinkReqBean) {
        if (Objects.isNull(userVipRealmLinkReqBean) || Objects.isNull(userVipRealmLinkReqBean.getArticleId())) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(3);

//        ArticleInfo articleInfo = articleInfoService.getArticleDetailInfo(userVipRealmLinkReqBean.getArticleId());

        String realm = "";
        String linkRealm = "";

        Integer vipStatus = userInfoService.getUserVipStatus(Integer.valueOf(userId));

        SysRealmMessage sysRealmMessage = this.sysGzhService.getGzhCodeRealmByVipStatus(springProfilesActive);

        if (Objects.nonNull(sysRealmMessage)) {
            if (vipStatus.intValue() == 1) {
                realm = sysRealmMessage.getVipRealm();// 会员推送域名
                linkRealm = sysRealmMessage.getVipLinkRealm();// 会员落地域名
            } else {
                realm = sysRealmMessage.getRealm();// 非会员推送域名
                linkRealm = sysRealmMessage.getLinkRealm();// 非会员落地域名
            }
        }

//        resultMap.put("articleTitle", articleInfo.getArticleTitle());
        resultMap.put("articleTitle", "");
        resultMap.put("realm", realm);
        resultMap.put("linkRealm", linkRealm);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20210904 hyl
     * 查询当前用户是否在屏蔽名单中
     *
     * @param
     * @return
     */
    @PostMapping(value = "/getUserShield", produces = "application/json;charset=UTF-8")
    public RespData getUserShield(@RequestHeader(value = "userId", required = false) String userId, @RequestHeader("identifying") String springProfilesActive) {

        Integer have = 0;

        if(userId!=null) {
            BUserShieldEntity use = this.shieldService.getUserShield(Integer.parseInt(userId));

            if (use != null && use.getUserId()!=null) {
                have = 1;
            }
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("have", have);//是否在名单中

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20210904 hyl
     * 查询当前文章是否在屏蔽名单中
     *
     * @param
     * @return
     */
    @PostMapping(value = "/getArticleShield", produces = "application/json;charset=UTF-8")
    public RespData getArticleShield(@RequestHeader(value = "userId", required = false) String userId, @RequestHeader("identifying") String springProfilesActive, @RequestBody GetArticleDetailInfoReqBean articleDetailInfoReqBean) {

        Integer have = 0;

        if(articleDetailInfoReqBean!=null) {
            BArticleShieldEntity articleShield = this.shieldService.getArticleShield(articleDetailInfoReqBean.getArticleId());

            if (articleShield != null && articleShield.getArticleId()!=null) {
                have = 1;
            }
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("have", have);//是否在名单中

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 查询当前文章的品牌是否在屏蔽名单中
     * @param
     * @return
     */
    @PostMapping(value = "/getBrandShield", produces = "application/json;charset=UTF-8")
    public RespData getBrandShield(@RequestHeader(value = "userId", required = false) String userId, @RequestHeader("identifying") String springProfilesActive, @RequestBody GetArticleDetailInfoReqBean articleDetailInfoReqBean) {

        Integer have = 0;

        if(!Objects.isNull(articleDetailInfoReqBean) && !Objects.isNull(articleDetailInfoReqBean.getArticleId())) {
            ArticleInfo articleInfo = this.articleInfoService.getArticleDetailInfo(articleDetailInfoReqBean.getArticleId());

            if (!Objects.isNull(articleInfo) && !Objects.isNull(articleInfo.getArticleBrandId())) {
                BBrandShieldEntity brandShield = this.shieldService.getBrandShield(articleInfo.getArticleBrandId(), springProfilesActive);

                if (brandShield != null && brandShield.getBrandId() != null && brandShield.getGzhCode() != null) {
                    have = 1;
                }
            }
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("have", have);//是否在名单中

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 查询当前公众号配置的版本号
     * @param
     * @return
     */
    @GetMapping(value = "/version", produces = "application/json;charset=UTF-8")
    public RespData version(@RequestHeader("identifying") String springProfilesActive) {
        PlatformSysInfo platformSysInfo = sysGzhService.getPlatformSysInfo(springProfilesActive);
        SysGzh sysGzh = platformSysInfo.getSysGzh();
        Integer version = sysGzh.getSleepTime();// 当前公众号的版本号

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("version", version);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 获取公众号的轮播图
     * @param springProfilesActive 公众号标识
     * @return 公众号的轮播图列表
     */
    @GetMapping(value = "/slideshow", produces = "application/json;charset=UTF-8")
    public RespData slideshow(@RequestHeader("identifying") String springProfilesActive) {
        List<SlideshowCombinedEntity> slideshowList = sysGzhService.getGzhSlideshow(springProfilesActive);

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("slideshowList", slideshowList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }


}
